//练习使用库函数，qsort排序各种类型的数据


#include<stdlib.h>
#include<stdio.h>
#include<string.h>
void Print(int* p, int sz)
{
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%d ", *(p + i));
	}
	printf("\n");
}

//比较整形数组
//int comper(const void* p1, const void* p2)
//{
//	return *((int*)p1) - *((int*)p2);
//}

//比较字符串
//int comper(const void* p1, const void* p2)
//{
//	return *(char*)p1 - *(char*)p2;
//}

typedef struct STU
{
	char name[20];
	int age;
}STU;

//int comper_age(const void* p1, const void* p2)
//{
//	return ((STU*)p1)->age - ((STU*)p2)->age;
//}

int comper_name(const void* p1, const void* p2)
{
	return strcmp(((STU*)p1)->name , ((STU*)p2)->name);
}


int main()
{
	//qsort整形数组
	//int arr[] = { 1,2,5,8,6,9,6,5,1 };
	//int sz= sizeof(arr) / sizeof(int);
	//Print(arr,sz);
	//qsort(arr, sz, sizeof(int), comper);
	//Print(arr, sz);

	//qsort字符串
	//char str[] = "aefbcdehj";
	//qsort(str, strlen(str), sizeof(char), comper);
	//printf("%s", str);

	//qsort结构体
	STU s[] = { {"zhansan",50},{"lisi",30},{"wangwu",40}};
	int sz = sizeof(s)/sizeof(STU);
	//qsort(s, sz, sizeof(STU), comper_age);
	//for (int i = 0; i < 3; i++)
	//{
	//	printf("%d ", s[i].age);
	//}

	//qsort结构体by name
	qsort(s, sz, sizeof(STU), comper_name);
	for (int i = 0; i < 3; i++)
	{
		printf("%s ", s[i].name);
	}
	return 0;
}